<?php
namespace lib;

class Url{

    /**
     * 返回连接
     * @param $option
     * @param $params
     * @return string
     */
    static function url($option, $params = false){
        if(empty($option)){
            $option = MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME;
        }else{
            substr_count($option, '/') <= 1 && $option = MODULE_NAME . "/{$option}";
        }
        $url = "http://{$_SERVER['HTTP_HOST']}" . WEB_ROOT . "/{$option}";
        if(is_array($params)){
            $paramArr = array();
            foreach($params as $key => $val){
                array_push($paramArr, "{$key}/{$val}");
            }
            $url = $url . '/' . implode('/', $paramArr);
        }
        return $url;
    }

    /**
     * 重定向
     * @param $url
     */
    static function redirect($url){
        stripos($url, 'http://') || $url = self::url($url);
        Header("Location: {$url}");
        exit;
    }

}